layui.use(['element', 'table', 'tree', 'zlpoptree', 'zlContext', 'zlConfig', 'treeGrid', 'zlPopSourceForm', 'zlPinYin', 'zlSelectEx'], function (exports) {
  $ = layui.$;
  var $ = layui.$
    , zlContext = layui.zlContext
    , element = layui.element
    , layer = layui.layer
    , table = layui.table
    , zlComponentRender = layui.zlComponentRender
    , zlConfig = layui.zlConfig
    , statusName = zlConfig.global.result.statusName
    , msgName = zlConfig.global.result.msgName
    , dataName = zlConfig.global.result.dataName
    , ERROR = zlConfig.global.result.errorState
    , OK = zlConfig.global.result.okState
    , form = layui.form
    , treeGrid = layui.treeGrid
    , fullheight = zlContext.setMaxHeihgt(0);
  $(window).on('resize', function () {
    zlContext.setMaxHeihgt(0);
  }).resize();

  var setting = {
    view: {
      showLine: true
    },
    async: {
      enable: true,
      url: layui.cache['contentPath'] + "/manager/corefunction/queryOrgddata",
      type: "get",
      dataFilter: function ajaxDataFilter(treeId, parentNode, responseData) {
        if (responseData[statusName] == OK) {
          var data = responseData[dataName];
          for (var i = 0; i < data.length; i++) {
            data[i].icon = layui.cache['contentPath'] + "/static/image/org.png";
          }
          return data;
        }
        else {
          zlContext.errorMsg(responseData[msgName]);
        }
        return responseData;
      }
    },
    data: {
      key: {
        name: "OBJNAME"
      }
      , simpleData: {
        enable: true
        , idKey: "RWID"
        , pIdKey: "SYS_PARENTID"
        , rootPid: ""
      }
    },
    callback: {
      onClick: function (event, treeId, treeNode) {
        $('input[name="ORGIDTEMP"]').val(treeNode.RWID);
        treeGrid.reload('treeTable', {
          url: layui.cache['contentPath'] + '/manager/coremenu/query?orgid=' + $('input[name="ORGIDTEMP"]').val()
          , height: fullheight
        });
      }
    }
  };
  $.fn.zTree.init($("#orgtree"), setting, null);

  var json = [
    {
      "id": "15D5AE6247D34967A068409BAA6D09E8",
      "name": "general",
      "xtype": "fromPanel",
      "titile": "通用设置",
      "SystemName": "快搭科技(上海)开发平台"
    },
    {
      "id": "3262C631487D498295F6234E68F33C2F",
      "name": "security",
      "titile": "安全",
      "xtype": "fromPanel",
      "Turn_On_password_Strength_Checking": {
        "id": "8162FC3D73374DE0A712BE63AE60814C",
        "name": "密码强度",
        "enable": 1,
        "value": "NL",
        "minlen": 1,
        "maxlen": 16,
        "xtype": "combox",
        "options": [
          { "value": "NL", "title": "数字字母（默认）" },
          { "value": "NL", "title": "数字特殊符号字母" },
          { "value": "NL", "title": "数字大小写特殊符号" },
        ]
      },
      "Turn_On_Number_Of_Error_Password_Allowances": {
        "id": "5CEA4FDD0BA44BBFA536857E4B71EE37",
        "name": "错误密码允许次数",
        "xtype": "checkboxGroup",
        "enable": 1,
        "value": 5,
        "lockTime": {
          "id": "2FE36E12D1D447E4B16C28A6A97F7C17",
          "name": "错误后锁定多少分钟",
          "value": 5,
          "xtype": "number",
        }
      },
      "Turn_On_Login_Validity_Period": {
        "id": "0F06B6FE8EB941DDB0CEB0F80B08B0E0",
        "name": "登录有效期",
        "enable": 1,
        "xtype": "checkboxGroup",
        "Re-login_as_soon_as_you_are_free": {
          "id": "8C8AD981A1C34455BDBBF1AE81770AFE",
          "name": "空闲多久就重新登录",
          "enable": 1,
          "value": 2,
          "xtype": "number",
        },
        "login_How_soon_will_you_login_again": {
          "id": "CF46771C3FCB43A5BDB83E9D786DF324",
          "name": "持续多久就重新登录",
          "enable": 0,
          "value": 2,
          "xtype": "number",
        }
      },
      "Turn_On_Calidation_Code": {
        "id": "E28D30D2F06B44A4AD54D6E993E161F4",
        "name": "开启验证码",
        "enable": 1,
        "xtype": "combox",
        "options": [
          { "value": "none", "title": "无验证码" },
          { "value": "simple", "title": "简单验证码（默认）" },
          { "value": "commonly", "title": "中等验证码" },
          { "value": "complex", "title": "复杂验证码" },
        ]
      },
      "Turn_On_Mobile_Phone_Dynamic_Verification": {
        "id": "63FD47A9481C495BAC5DF21FEF084886",
        "name": "手机动态验证机制",
        "enable": 0,
        "xtype": "checkboxGroup",
        "value": "albaba"
      },
      "Turn_On_The_Forget_Password": {
        "id": "C8CA5DACD46A4168821F2EC4F671101C",
        "name": "忘记密码",
        "enable": 0
        , "xtype": "checkbox"
      },
      "Turn_On_Monitoring_Abnormal_Behavior": {
        "id": "08E54121AB5E4F2C8C49C3F7135105E5",
        "xtype": "checkboxGroup",
        "name": "监控异常行为",
        "enable": 0,
        "Log_in_more_than_N_times_in_a_minute_and_blacklist_IP": {
          "id": "DE62A6000B2F4E59A57F64464BE1871B",
          "name": "一分钟内登录次数超过N次，把IP加入黑名单",
          "enable": 0,
          "value": 10,
          "xtype": "number"
        }
      },
      "Turn_On_Record_the_last_login_username": {
        "id": "75910F47E05A48348FD046B6BC2DCE95",
        "xtype": "checkbox",
        "name": "记录最后一次登录用户名",
        "enable": 1
      },
      "Turn_On_Preventive_Account_Against_Theft": {
        "id": "72E9620A81C04025BB10B56157C3E124",
        "xtype": "checkboxGroup",
        "name": "账号安全",
        "Time_to_change_passwords": {
          "xtype": "checkbox",
          "name": "密码连续几个月没修改，密码，建议修改密码",
          "enable": 0,
          "value": 30
        },
        "Only_one_login_at_a_time": {
          "id": "E4C75E52D2454FCF95DD93A9ECBB37B0",
          "xtype": "checkbox",
          "name": "同一用户同一时间只能登录一个",
          "enable": 0
        },
        "How_long_has_it_been_since_I_logged_in_to_change_my_password": {
          "id": "1C22925E87814FD4A46A6C187E4C993B",
          "xtype": "number",
          "name": "两次登录时间超过多久以上，建议修改密码",
          "enable": 0,
          "value": 60
        }
      },
      "Turn_On_A_Single_Browser_to_log_in_to_multiple_accounts": {
        "id": "00D668B96AF04589A1AF23D9E3CEE59C",
        "xtype": "checkbox",
        "name": "开启单浏览器登录多账户",
        "enable": 0
      },
      "Turn_On_Account_Password_Transfer_Encryption": {
        "id": "12B7DAF971A34C81ACEB5FEA357D8132",
        "xtype": "checkbox",
        "name": "账户密码传输加密",
        "enable": 1
      },
      "Turn_On_Initial_password_of_account": {
        "id": "C349E6C1797F4854922F6F49EC0300F7",
        "xtype": "textbox",
        "name": "账户初始密码",
        "enable": 1,
        "value": "123456"
      }
    },
    {
      "id": "F2700E12F56A4AEABBECE56F0AD447CB",
      "name": "Option_20",
      "titile": "web2.0选项",
      "xtype": "fromPanel"
    }
  ];


  var Clazz = function () { };
  Clazz.prototype.construct = function () { };
  Clazz.extend = function (def) {
    var classDef = function () {
      if (arguments[0] !== Clazz) { this.construct.apply(this, arguments); }
    };
    var proto = new this(Clazz);
    var superClass = this.prototype;
    for (var n in def) {
      var item = def[n];
      if (item instanceof Function) item.$ = superClass;
      proto[n] = item;
    }
    classDef.prototype = proto;
    classDef.extend = this.extend;
    return classDef;
  };
  var componentBase = Clazz.extend({
    id: "",
    name: "",
    xtype: "",
    $elem: undefined,
    construct: function (json) {
      $.extend(this, json);
    },
    render: function () {
    }
  });
  var containerBase = componentBase.extend({
    addchild: function (json) {
    }
  });
  var inputBase = componentBase.extend({
  });
  var checkboxGroup = containerBase.extend({
    enable: true,
    render: function () {
      this.$elem = $([
        '<fieldset class="layui-elem-field" style="margin-top: 30px;">'
        , '   <legend><input type="checkbox" name="' + this.name + '" lay-skin="primary" checked="">' + this.name + '</legend>'
        , '  <div class="child_'+this.id+'">'
        , '  </div>'
        , '</fieldset>'
      ].join(''))
      return this;
    },
    addchild: function (cmp) {
      this.$elem.find(".child_"+this.id).append(cmp.$elem);
    }
  });
  var fromPanel = containerBase.extend({
    render: function () {
      this.$elem = $([
        '<from class="layui-form" action=""></from>'
      ].join(''))
      return this;
    },
    addchild: function (cmp) {
      this.$elem.append(cmp.$elem);
    }
  });
  var checkbox = inputBase.extend({
    render: function () {
      this.$elem = $([
        '<div class="layui-form-item">'
        , '  <label class="layui-form-label">' + this.name + '</label>'
        //, '  <div class="layui-input-block">'
        , '    <input type="checkbox" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">'
        //, '  </div>'
        , '</div>'
      ].join(''))
      return this;
    }
  });
  var number = inputBase.extend({
    render: function () {
      this.$elem = $([
        '<div class="layui-form-item">'
        , '  <label class="layui-form-label">' + this.name + '</label>'
        //, '  <div class="layui-input-block">'
        , '    <input type="text" name="' + this.name + '" lay-verify="number" placeholder="输入' + this.name + '" autocomplete="off" class="layui-input">'
        //, '  </div>'
        , '</div>'
      ].join(''))
      return this;
    }
  });
  var combox = inputBase.extend({
    render: function () {
      var arrOption = [];
      for (var i = 0; i < this.options.length; i++) {
        arrOption.push('<option value="' + this.options[i].value + '">' + this.options[i].title + '</option>');
      }
      this.$elem = $([
        '<div class="layui-form-item">'
        , '  <label class="layui-form-label">' + this.name + '</label>'
        //, '  <div class="layui-input-block">'
        , '    <select name="' + this.name + '">'
        , arrOption.join('')
        , '    </select>'
        //, '  </div>'
        , '</div>'
      ].join(''))
      return this;
    }
  });
  var textbox = inputBase.extend({
    render: function () {
      this.$elem = $([
        '<div class="layui-form-item">'
        , '  <label class="layui-form-label">' + this.name + '</label>'
        //, '  <div class="layui-input-block">'
        , '     <input type="text" name="' + this.name + '" lay-verify="number" placeholder="输入' + this.name + '" autocomplete="off" class="layui-input">'
        //, '  </div>'
        , '</div>'
      ].join(''))
      return this;
    }
  });

  var formRender = {
    render: function (json, ElemId) {
      var $elem = $("#" + ElemId);
      var forJson = function (parentJson, parentCmpObj) {
        for (var skey in parentJson) {
          var itemJson = parentJson[skey];
          if (itemJson && typeof (itemJson) == "object" && itemJson.xtype) {
            var xtypeObj = undefined;
            var xtypeclass = eval('(' + itemJson.xtype + ')');
            var xtypeObj = new xtypeclass(itemJson);
            xtypeObj.render();
            if (parentCmpObj && parentCmpObj.addchild) {
              parentCmpObj.addchild(xtypeObj);
              forJson(itemJson, parentCmpObj);
            } else {
              forJson(itemJson, parentCmpObj);
            }
          }
        }
      }

      var rootclass = eval('(' + json.xtype + ')');
      var rootobj = new rootclass(json);
      rootobj.render();
      $elem.append(rootobj.$elem);
      forJson(json, rootobj);

    }
  }
  var syssetting = {
    view: {
      showLine: true
    },
    data: {
      key: {
        name: "titile"
      }
    },
    callback: {
      onClick: function (event, treeId, treeNode) {
        $("#showHere").empty();
        formRender.render(treeNode, "showHere");
        form.render();
      }
    }
  }
  $.fn.zTree.init($("#catlogtree"), syssetting, json);
});


